<?php
// +----------------------------------------------------------------------
// | ITKEE [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.itkee.cn.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: SuperMan <superman@itkee.cn>
// +----------------------------------------------------------------------
// | USER 模型
// +----------------------------------------------------------------------
namespace app\common\model;

use think\Db;
use think\Model;
use think\Session;
use think\Config;

/**
 * Class User
 * @package app\common\model
 * 用户model
 * author lixy
 */
class User extends Model
{



    protected function getIntroAttr($value)
    {
        return $value?$value:'这家伙什么都不说！^_^';
    }

    /**后台添加系统用户
     * @param $data
     * @return bool
     */
    public static function addUser($data)
    {
        $time = time();
        Db::startTrans();
        try {
            $id = Db::name('user')->insertGetId([
                'u_email'        => $data['u_email'],
                'u_password'     => $data['u_password'],
                'u_type'         => 2,
                'u_created_time' => $time
            ]);
            $u_code  = $id + 100000;
            Db::name('user')->where('u_id', $id)->update(['u_code' =>  $u_code, 'u_update_time' => $time]);
            Db::commit();
            return true;
        } catch(\Exception $e){
            Db::rollback();
            return false;
        }
    }

    /**
     *用户的证券收益情况
     */
    public static function userSecuritiesIncome($uid)
    {
        $detail = Db::name('project')->where('p_status', 1)->field('p_id, p_name,p_photo, p_num')->order('p_id ASC')->select();
        foreach ($detail as $k => $v) {
            $detail[$k]['p_photo'] = 'http://'.$_SERVER['HTTP_HOST'].$v['p_photo'];
            $data = Db::name('user_securities')->where('us_uid', $uid)->where('us_pid', $v['p_id'])->field('us_securities_money, us_yesterday_earnings')->find();
            if ($data) {
                $detail[$k]['us_securities_money'] = $data['us_securities_money'];
                $detail[$k]['us_yesterday_earnings'] = $data['us_yesterday_earnings'];
            } else {
                $detail[$k]['us_securities_money'] = '0.00';
                $detail[$k]['us_yesterday_earnings'] = '0.00';
            }
        }

        return $detail;
    }


//    public static function offlinelist($uid)
//    {
//        $uidtwo = Db::name('user')->where('u_superior', $uid)->field('u_id, u_email')->select();
//        if (sizeof($uidtwo) > 0) {//二级推广用户
//            $detail = Db::name('project')->where('p_status', 1)->field('p_id, p_name,p_photo')->select();
//            $level = Db::name('systems')->where('s_id', 1)->field('s_second_level, s_third_level, s_fouth_level')->find();
//            foreach($detail as $k => $v){
//                foreach($uidtwo as $key => $val){
//                    $uidtwo['p_name'] = $v['p_name'];
//                    $uidtwo['erjishouyi'] = Db::name('user_securities')
//                        ->where('us_uid', $val['u_id'])
//                        ->where('us_pid', $v['p_id'])
//                        ->sum('us_yesterday_earnings');
//                    $uidtwo['erjixiaoyi'] = floatval($uidtwo['erjishouyi']) * floatval($level['s_second_level']) / 100;//昨日该用户对我产生的效益
//                }
//            }
//        } else {
//            return false;
//        }
//    }
    /**推广用户的列表信息
     * @param $uid
     * @param $page
     */
    public static function offlinelist($uid)
    {
        $level = Db::name('systems')->where('s_id', 1)->field('s_second_level, s_third_level, s_fouth_level')->find();
        $secondlevel = $level['s_second_level'];
        $thirdlevel = $level['s_third_level'];
        $fouthlevel = $level['s_fouth_level'];
        $list = Db::name('user')->where('u_superior', $uid)->field('u_id, u_email, from_unixtime(u_created_time) u_created_time')->select();
        if (sizeof($list) > 0) {
            //二级用户 推广的信息
            $threeArr = [];//二级用户的数组UID
            foreach ($list as $k => $v) {
                array_push($threeArr, $v['u_id']);
                $earnings = Db::name('user_securities')->where('us_uid', $v['u_id'])->sum('us_yesterday_earnings');
                if ($earnings) {
                    $list[$k]['us_yesterday_earnings'] =sprintf("%.2f", $earnings);
                    $list[$k]['user_two_earnings'] = sprintf("%.2f", floatval($list[$k]['us_yesterday_earnings'])*floatval($secondlevel)/100);
                } else {
                    $list[$k]['us_yesterday_earnings'] = '0.00';
                    $list[$k]['user_two_earnings'] = '0.00';
                }
            }

            //三级用户
            $lista =  Db::name('user')->where('u_superior', 'in', $threeArr)->field('u_id, u_email, from_unixtime(u_created_time) u_created_time')->select();
            if (sizeof($lista) > 0) {
                $fourArr = [];//四级用户的数组UID
                foreach($lista as $k => $v){
                    array_push($fourArr, $v['u_id']);
                    $earnings = Db::name('user_securities')->where('us_uid', $v['u_id'])->sum('us_yesterday_earnings');
                    if ($earnings) {
                        $lista[$k]['us_yesterday_earnings'] =sprintf("%.2f", $earnings);
                        $lista[$k]['user_two_earnings'] = sprintf("%.2f", floatval($lista[$k]['us_yesterday_earnings'])*floatval($thirdlevel)/100);
                    } else {
                        $lista[$k]['us_yesterday_earnings'] = '0.00';
                        $lista[$k]['user_two_earnings'] = '0.00';
                    }
                }
                //四级用户
                $listb =  Db::name('user')->where('u_superior', 'in', $fourArr)->field('u_id, u_email, from_unixtime(u_created_time) u_created_time')->select();
                if (sizeof($listb) > 0) {
                    foreach($listb as $k => $v){
                        $earnings = Db::name('user_securities')->where('us_uid', $v['u_id'])->sum('us_yesterday_earnings');
                        if ($earnings) {
                            $listb[$k]['us_yesterday_earnings'] =sprintf("%.2f", $earnings);
                            $listb[$k]['user_two_earnings'] = sprintf("%.2f", floatval($listb[$k]['us_yesterday_earnings'])*floatval($fouthlevel)/100);
                        } else {
                            $listb[$k]['us_yesterday_earnings'] = '0.00';
                            $listb[$k]['user_two_earnings'] = '0.00';
                        }
                    }
                } else {
                    $listb = null;
                }

            } else {
                $lista = null;
                $listb = null;
            }
            $data['list']= $list;
            $data['lista']= $lista;
            $data['listb']= $listb;

        } else {
            $data['list']= null;
            $data['lista']= null;
            $data['listb']= null;
        }
        return $data;
    }

    /**
     * 下线总充值额度
     */
    public static function downUserAllMoney($uid)
    {
        $two = Db::name('user')->where('u_superior', $uid)->value('group_concat(u_id) u_id');
        $money = Db::name('order')->where('o_uid', $uid)->where('o_status', 2)->sum('o_money_dollar');
        if ($two) {
            $twoArr = explode(',', $two);
            $money2 = Db::name('order')->where('o_uid', 'in', $twoArr)->where('o_status', 2)->sum('o_money_dollar');
            $three = Db::name('user')->where('u_superior', 'in', $twoArr)->value('group_concat(u_id) u_id');
            if ($three) {
                $threeArr = explode(',', $three);
                $money3 = Db::name('order')->where('o_uid', 'in', $threeArr)->where('o_status', 2)->sum('o_money_dollar');
                $four = Db::name('user')->where('u_superior', 'in', $threeArr)->value('group_concat(u_id) u_id');
                if ($four) {
                    $fourArr = explode(',', $four);
                    $money4 = Db::name('order')->where('o_uid', 'in', $fourArr)->where('o_status', 2)->sum('o_money_dollar');
                    return number_format(floatval($money) + floatval($money2) + floatval($money3) + floatval($money4), 2);
                }else {
                    return number_format(floatval($money) + floatval($money2) + floatval($money3),2);
                }
            } else {
                return number_format(floatval($money) + floatval($money2), 2);
            }
        } else {
            return number_format(floatval($money),2);
        }

    }
    /**
     * 用户自己每天证券进行收益<定时器用到>
     */
    public static function userProjectProfit()
    {
        $data = Db::name('user_securities')->field('us_id, us_uid, us_pid, us_securities_money')->select();
        if (sizeof($data) > 0) {
            foreach($data as $k=>$v){
                $profit = Db::name('project')->where('p_id', $v['us_pid'])->value('p_profit');//证券的收益利率
                //该证券的收益
                $money = floatval($v['us_securities_money'])*floatval($profit)/100;
                Db::name('user_securities')->where('us_id', $v['us_id'])->setInc('us_securities_money', $money);
                Db::name('user_securities')->where('us_id', $v['us_id'])->update([
                    'us_yesterday_earnings'   => $money,
                    'us_update_time'          => time()
                ]);
                //给用户累加收益
                Db::name('user')->where('u_id', $v['us_uid'])->setInc('u_all_profit', $money);
                Db::name('user')->where('u_id', $v['us_uid'])->setInc('u_all_income', $money);
            }
        }
        return true;
    }
    /**
     * 获取 所有买过证券的用户ID 《定时器用到》
     */
    public static function getBuyProjectUid()
    {
        return Db::name('user_securities')->group('us_uid')->field('us_uid')->select();
    }

    /**《定时器用到》
     *根据用户的ID  查询该昨日所有证券的收益总额  然后在向上级分不同的收益利率给上级添加收益
     */
    public static function upwardRebate($uid)
    {
        $allearnings = Db::name('user_securities')->where('us_uid', $uid)->sum('us_yesterday_earnings');
        $secUid = Db::name('user')->where('u_id', $uid)->value('u_superior');
        if ($secUid) {
            //给上级返利
            $level = Db::name('systems')->where('s_id', 1)->field('s_second_level, s_third_level, s_fouth_level')->find();
            $twoMoney = floatval($allearnings)*floatval($level['s_second_level'])/100;
            Db::name('user')->where('u_id', $secUid)->setInc('u_dollar_balance', $twoMoney);
            Db::name('user')->where('u_id', $secUid)->setInc('u_all_profit', $twoMoney);
            $thiUid = Db::name('user')->where('u_id', $secUid)->value('u_superior');
            if ($thiUid) {
                //给上上级返利
                $threeMoney = floatval($allearnings)*floatval($level['s_third_level'])/100;
                Db::name('user')->where('u_id', $thiUid)->setInc('u_dollar_balance', $threeMoney);
                Db::name('user')->where('u_id', $thiUid)->setInc('u_all_profit', $threeMoney);

                $fouUid = Db::name('user')->where('u_id', $thiUid)->value('u_superior');
                if ($fouUid){
                    //给上上上级返利
                    $fourMoney = floatval($allearnings)*floatval($level['s_fouth_level'])/100;
                    Db::name('user')->where('u_id', $fouUid)->setInc('u_dollar_balance', $fourMoney);
                    Db::name('user')->where('u_id', $fouUid)->setInc('u_all_profit', $fourMoney);
                }
            }
        }
        return true;
    }

    /**
     * 重置密码
     */
    public static function resetpassword($uid)
    {
        $u_password = md5(Config::get('salt') . 123456);
        return Db::name('user')->where('u_id', $uid)->update(['u_password' => $u_password, 'u_update_time' => time()]);

    }
    /**==================================================================================================================================
     * 前端注册
     * @param $data
     * @return bool
     */

    public static function registerUser($data)
    {
        $data['u_password'] = md5(Config::get('salt') . $data['u_password']);
        $time = time();
        Db::startTrans();
        try {
            $uid = Db::name('user')->insertGetId([
                'u_email'        =>  $data['u_email'],
                'u_password'     =>  $data['u_password'],
                'u_superior'     =>  $data['u_superior'],
                'u_created_time' =>  $time,
                'u_update_time'  =>  $time,
            ]);
            $u_code  = $uid + 100000;
            Db::name('user')->where('u_id', $uid)->update([
                'u_code'         => $u_code,
                'u_update_time'  =>  $time,
            ]);
            Db::commit();
            return true;
        } catch(\Exception $e){
            Db::rollback();
            return false;
        }
    }
    /**
     * 找回密码
     */
    public static function forgetPassword($data)
    {
        $password = md5(Config::get('salt') . $data['u_password']);
        return Db::name('user')->where('u_email', $data['u_email'])->update([
            'u_password'    => $password,
            'u_update_time' => time(),
        ]);
    }

    /**登录
     * @param $data
     * @return array|bool|false|\PDOStatement|string|Model
     */
    public static function loginUser($data)
    {
        $pwd = md5(Config::get('salt') . $data['u_password']);
        $data = Db::name('user')
            ->where('u_email', $data['u_email'])
            ->find();
        if ($data) {
            if ($data['u_type'] != 1) {
                return -4;
            }
            if ($data['u_password'] != $pwd) {
                return -2;
            }
            if ($data['u_status'] != 1) {
                return -3;
            }
            unset($data['u_password']);
            unset($data['u_cash_transfer_password']);
            return $data;
        } else {
           return -1;
        }
    }
    /**
     * 查询邀请码是不是存在
     */
    public static function getCode($code)
    {
        return Db::name('user')->where('u_code', $code)->value('u_id');

    }

    /**
     * 验证邮箱是不是注册过了
     */
    public static function emailRepace($email)
    {
        return Db::name('user')->where('u_email', $email)->value('u_id');
    }

    /**
     * 根据用户ID 获取各种的列表记录（转账1   充值2  提现3）
     */
    public static function getUserRecord($uid, $type, $page)
    {
        $list = [];
        if ($type == 1) {
            $list = Db::name('transfer_record')
                ->join('user one', 'tr_out_uid = one.u_id', 'left')
                ->join('user two', 'tr_enter_uid = two.u_id', 'left')
                ->join('project', 'tr_pid = p_id', 'left')
                ->where('tr_out_uid', $uid)
                ->whereOr('tr_enter_uid', $uid)
                ->field('tr_id, one.u_email as oneemail, two.u_email as twoemail, tr_type, p_name, tr_market_value, tr_dollar,
             from_unixtime(tr_create_time) tr_create_time, tr_out_uid, tr_enter_uid')
                ->order('tr_id DESC')
                ->paginate(15, false, ['page' => $page]);
        } elseif($type == 2) {
            $list = Db::name('order')
                ->join('user', 'o_uid = u_id', 'left')
                ->where('o_uid', $uid)
                ->field('o_id, u_email, o_order_num,o_type, o_money_dollar,o_status, from_unixtime(o_update_time) o_update_time')
                ->order('o_id DESC')
                ->paginate(15, false, ['page' => $page]);
        }elseif($type == 3){
            $list = Db::name('drawing')
                ->join('user', 'd_uid = u_id', 'left')
                ->where('d_uid', $uid)
                ->field('d_id, u_email, d_type, d_money_dollar, d_status,from_unixtime(d_update_time) d_update_time')
                ->order('d_id DESC')
                ->paginate(15, false, ['page' => $page]);
        }
        if (sizeof($list) > 0) {
            return $list;
        }
        return false;
    }


    /**
     * 查询用户是否存在
     */
    public static function emailUser($uid, $email)
    {
        $res = Db::name('user')->where('u_id', $uid)->where('u_email', $email)->value('u_id');
        if ($res) {
            return  -1;
        }
        $status = Db::name('user')->where('u_email', $email)->value('u_id');
        if (!$status) {
            return -2;
        }
        return $email;
    }

    /**
     * 根据用户ID 查询该用户拥有证券的列表
     */
    public static function getUserHaveProjectList($uid)
    {
        $list = Db::name('user_securities')
            ->join('project', 'us_pid = p_id')
            ->where('us_uid', $uid)
            ->field('us_id, p_name, p_photo, us_securities_money, us_yesterday_earnings')
            ->select();
        if (sizeof($list) > 0) {
            foreach($list as $k=>$v){
                $list[$k]['p_photo'] = 'http://'.$_SERVER['HTTP_HOST'].$v['p_photo'];
            }
            return $list;
        }
        return false;
    }

    /**
     *用户转证券
     */
    public static function TurnOutProject($data)
    {
        $securities = Db::name('user_securities')->where('us_id', $data['us_id'])->where('us_uid', $data['u_id'])
            ->field('us_pid, us_securities_money')->find();
        if (!$securities['us_securities_money']) {
            return -1;//未知错误
        }
        if (floatval($securities['us_securities_money']) < floatval($data['money'])) {
            return -2;//转出证券的市值  大于 用户拥有证券的市值
        }
        $pwd = md5(Config::get('salt') . $data['password']);
        $password = Db::name('user')
            ->where('u_id', $data['u_id'])
            ->value('u_cash_transfer_password');
        if ($password != $pwd) {
            return -3;
        }
        $outUid = Db::name('user')->where('u_email', $data['u_email'])->value('u_id');
        if (!$outUid) {
            return -4;
        }
        //查询被转出人是否拥有该证券
        $usid = Db::name('user_securities')->where('us_uid', $outUid)->where('us_pid', $securities['us_pid'])->value('us_id');
        $time = time();

        if ($usid) {//被转出人有证券
            Db::startTrans();
            try {
                Db::name('user_securities')->where('us_id', $data['us_id'])->setDec('us_securities_money', $data['money']);
                Db::name('transfer_record')->insertGetId([
                    'tr_out_uid'     => $data['u_id'],
                    'tr_enter_uid'   => $outUid,
                    'tr_type'        => 1,
                    'tr_pid'         => $securities['us_pid'],
                    'tr_market_value'=> $data['money'],
                    'tr_create_time' => $time,
                    'tr_update_time' => $time,
                ]);
                Db::name('user_securities')->where('us_id', $usid)->setInc('us_securities_money', $data['money']);
                Db::commit();
                return true;
            } catch(\Exception $e){
                Db::rollback();
                return false;
            }
        } else {//被转出人没有证券
            Db::startTrans();
            try {
                Db::name('user_securities')->where('us_id', $data['us_id'])->setDec('us_securities_money', $data['money']);
                Db::name('transfer_record')->insertGetId([
                    'tr_out_uid'     => $data['u_id'],
                    'tr_enter_uid'   => $outUid,
                    'tr_type'        => 1,
                    'tr_pid'         => $securities['us_pid'],
                    'tr_market_value'=> $data['money'],
                    'tr_create_time' => $time,
                    'tr_update_time' => $time,
                ]);
                Db::name('user_securities')->insertGetId([
                    'us_uid'                => $outUid,
                    'us_pid'                => $securities['us_pid'],
                    'us_securities_money'   => $data['money'],
                    'us_create_time'        => $time,
                    'us_update_time'        => $time,
                ]);
                Db::commit();
                return true;
            } catch(\Exception $e){
                Db::rollback();
                return false;
            }
        }
    }

    /**
     * 用户转美金
     */
    public static function turnOutDollar($data)
    {
        $outUid = Db::name('user')->where('u_email', $data['u_email'])->value('u_id');
        if (!$outUid) {
            return -1;
        }
        $money = Db::name('user')->where('u_id', $data['u_id'])->value('u_dollar_balance');
        if (floatval($money) < floatval($data['money'])) {
            return -2;//转出金额大于拥有的金额
        }
        $pwd = md5(Config::get('salt') . $data['password']);
        $password = Db::name('user')
            ->where('u_id', $data['u_id'])
            ->value('u_cash_transfer_password');
        if ($password != $pwd) {
            return -3;
        }
        $time = time();
        Db::startTrans();
        try {
            Db::name('user')->where('u_id', $data['u_id'])->setDec('u_dollar_balance', $data['money']);
            Db::name('user')->where('u_id', $outUid)->setInc('u_dollar_balance', $data['money']);
            Db::name('transfer_record')->insertGetId([
                'tr_out_uid'     => $data['u_id'],
                'tr_enter_uid'   => $outUid,
                'tr_type'        => 2,
                'tr_dollar'      => $data['money'],
                'tr_create_time' => $time,
                'tr_update_time' => $time,
            ]);
            Db::commit();
            return true;
        } catch(\Exception $e){
            Db::rollback();
            return false;
        }
    }
    /**
     * 用户绑定 信息
     */
    public static function userBiddingForUid($data)
    {
        $data['u_update_time'] = time();
        $data['u_binding_status'] = 2;
        return Db::name('user')->where('u_id', $data['u_id'])
            ->update($data);
    }
    /**
     * 设置支付密码
     */
    public static function addTransferPassword($data)
    {
        $password =  md5(Config::get('salt') . $data['password']);
        return Db::name('user')->where('u_id', $data['u_id'])
            ->update([
                'u_cash_transfer_password' => $password,
                'u_update_time' => time(),
            ]);
    }


    /**
     * 修改密码 （1登录密码     2支付密码）
     */
    public static function savePassword ($data)
    {
        $oldPassword = md5(Config::get('salt') . $data['oldpassword']);
        $newPassword = md5(Config::get('salt') . $data['password']);
        if ($data['type']  == 1) {//修改登录密码
            $id = Db::name('user')->where('u_id', $data['u_id'])->where('u_password', $oldPassword)->value('u_id');
            if (!$id) {
                return -1;//旧密码输入错误
            }
            return Db::name('user')->where('u_id', $data['u_id'])
                ->update(['u_password' => $newPassword, 'u_update_time' => time()]);

        } elseif ($data['type']  == 2){//修改支付密码
            $id = Db::name('user')->where('u_id', $data['u_id'])->where('u_cash_transfer_password', $oldPassword)->value('u_id');
            if (!$id) {
                return -1;//旧密码输入错误
            }
            return Db::name('user')->where('u_id', $data['u_id'])
                ->update(['u_cash_transfer_password' => $newPassword, 'u_update_time' => time()]);
        }else{
            return false;
        }
    }
    /**
     * 证券转换美金
     */
//'u_id.require'           => '用户ID不能为空',
//'us_id.require'          => '请选择要转换的证券',
//'money.require'          => '市值不能为空',
    public static function toMoney($data)
    {
        $info = Db::name('user_securities')->where('us_id', $data['us_id'])->where('us_uid', $data['u_id'])->field('us_pid, us_securities_money')->find();
        if (!$info) {
            return false;
        }
        $money = $info['us_securities_money'];
        $pid   = $info['us_pid'];
        if (floatval($money) < floatval($data['money'])) {
            return -1;
        }
        $time = time();
        Db::startTrans();
        try {
            Db::name('user_securities')->where('us_id', $data['us_id'])->setDec('us_securities_money', $data['money']);
            Db::name('user')->where('u_id', $data['u_id'])->setInc('u_dollar_balance', $data['money']);
            Db::name('transfer_record')->insertGetId([
                'tr_out_uid'  => $data['u_id'],
                'tr_enter_uid'  => $data['u_id'],
                'tr_type'  => 3,
                'tr_pid'  => $pid,
                'tr_market_value' => $data['money'],
                'tr_create_time'  => $time,
                'tr_update_time'  => $time,
            ]);
            Db::commit();
            return true;
        } catch(\Exception $e){
            Db::rollback();
            return false;
        }
    }

    /**
     * 用户提现
     */
    public static function withdrawal($data)
    {
        $password = md5(Config::get('salt') . $data['password']);
        $info = Db::name('user')->where('u_id', $data['u_id'])->field('u_dollar_balance, u_cash_transfer_password, u_binding_status')->find();
        if (!$info) {
            return false;
        }
        if ($info['u_binding_status'] != 2) {
            return -3;
        }
        if (!$info['u_cash_transfer_password']) {
            return -4;
        }
        if ($info['u_cash_transfer_password'] != $password) {
            return -1;
        }
        if (floatval($data['money']) > floatval($info['u_dollar_balance'])){
            return -2;
        }
        $time = time();
        $systemData = Db::name('systems')->where('s_id', 1)->field('s_rmb_dollar_exchange, s_service_harge')->find();
        Db::startTrans();
        try {
            Db::name('user')->where('u_id', $data['u_id'])->setDec('u_dollar_balance', $data['money']);
            Db::name('user')->where('u_id', $data['u_id'])->update(['u_update_time' => $time]);
            Db::name('drawing')->insertGetId([
                'd_uid'          =>   $data['u_id'],
                'd_type'         =>   $data['type'],
                'd_money_rmb'    =>   floatval($data['money'])*floatval($systemData['s_rmb_dollar_exchange'])*(1-floatval($systemData['s_service_harge'])/100),
                'd_money_dollar' =>   $data['money'],
                'd_status'       =>   1,
                'd_remark'       =>   $data['remark'],
                'd_create_time'  =>   $time,
                'd_update_time'  =>   $time,
            ]);
            Db::commit();
            return true;
        } catch(\Exception $e){
            Db::rollback();
            return false;
        }
    }


    /**
     * 验证手机号是不是注册过了
     */
    public static function phoneRepace($phone)
    {
        return Db::name('user')->where('u_phone', $phone)->value('u_id');
    }

    /**根据 手机号 或者 邮箱 重置密码
     * @param $account  账号（手机号 邮箱）
     * @param $u_password 密码
     * @param $type == 1 根据邮箱重置密码    == 2根据手机号重置密码
     * @return bool|int|string
     * @throws \think\Exception
     */
    public static function resetPasswordForAccount($account, $u_password, $type)
    {
        $u_password = md5(Config::get('salt') . $u_password);
        if ($type == 1) {
            return Db::name('user')->where('u_email', $account)->update(['u_password' => $u_password, 'u_update_time' => time()]);
        } elseif ($type == 2) {
            return Db::name('user')->where('u_phone', $account)->update(['u_password' => $u_password, 'u_update_time' => time()]);
        } else {
            return false;
        }
    }
    /**
     * 根据用户ID 获取用户的基本信息
     */
    public static function getUserData($uid)
    {
       $data = Db::name('user')
           ->where('u_id', $uid)
           ->field('u_id, u_email, u_code, u_dollar_balance, u_alipay, u_bank_card_code, u_bank_type, u_name_bank, u_bank_phone, u_binding_status, u_all_profit, u_cash_transfer_password')
           ->find();
        $data['harge'] = Db::name('systems')->where('s_id', 1)->value('s_service_harge');
        if ($data['u_cash_transfer_password']) {
            $data['u_cash_transfer_password'] = 2;
        } else {
            $data['u_cash_transfer_password'] = 1;
        }

        return $data;

    }
    /**
     * 根据用户的ID  修改基本信息
     */
    public static function updateUserInfoByid($data)
    {
        if (array_key_exists("u_enterprise_time",$data) && $data['u_enterprise_time'] != '') {
            $data['u_enterprise_time'] = strtotime($data['u_enterprise_time']);
        }
        $data['u_update_time'] = time();
        return Db::name('user')->where('u_id', $data['u_id'])->update($data);
    }



    /**
     *查找关系用户信息
     */
    public function getUserInfo ($map)
    {
        $list = $this->where($map)->select();
        return $list;
    }

    /*
     * 根据用户名找出相关用户id
     */
    public function nameToId ($name)
    {
        $map['username'] = ['like', "%{$name}%"];
        $userId = $this->where($map)->find();
        return $userId;
    }


    /**
     * @param $map
     * 查找单个用户信息
     */
    public function getFindUser($map){
        $list = $this->where($map)->find();
        return $list;
    }


    /**
     * @param $map
     * @param $data
     * @return false|int
     * 更新用户信息
     */
    public function saveUserInfo($map,$data){
        $res = $this->where($map)->data($data)->save();
        return $res;
    }

}